package com.aaa.ddd.service;

import com.aaa.ddd.dao.MenuDao;
import com.aaa.pojo.LayUiTree;
import com.aaa.pojo.Menu;
import com.aaa.pojo.Page;
import com.aaa.pojo.ResultBean;

import com.aaa.utils.TreeUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class MenuServiceimpl implements MenuService {
    @Autowired
   private MenuDao menuDao;


    @Override
    public Map<String, List<Menu>> findMenuByUsername(String username){
        List<Menu> menuList = menuDao.findMenuByUsername(username);
        List<Menu> menuListlevel1 = new ArrayList<>();
        List<Menu> menuListlevel2 = new ArrayList<>();
        Iterator<Menu> iterator = menuList.iterator();
        while (iterator.hasNext()){
            Menu menu = iterator.next();
            if (menu.getMenu_type().equals("M")){
                menuListlevel1.add(menu);
            }
            if (menu.getMenu_type().equals("C")){
                menuListlevel2.add(menu);
            }
        }
        Map<String ,List<Menu>> map = new HashMap();
        map.put("menuList1",menuListlevel1);
        map.put("menuList2",menuListlevel2);
        return map;
    }

    public Menu queryById(Integer menuId) {
        return this.menuDao.queryById(menuId);
    }

    public List<Menu> queryAllByLimit(int offset, int limit) {
        return this.menuDao.queryAllByLimit(offset, limit);
    }

    public List<Menu> queryAllMenu() {
        return this.menuDao.queryAllMenu();
    }

    public Menu insert(Menu menu) {
        this.menuDao.insert(menu);
        List<Integer> roles = this.menuDao.selectAllRole();
        Iterator var3 = roles.iterator();

        while(var3.hasNext()) {
            Integer role = (Integer)var3.next();
            this.menuDao.insertRoleMenu(role, menu.getMenu_id());
        }

        return menu;
    }

    public Menu update(Menu menu) {
        this.menuDao.update(menu);
        this.menuDao.deleteRoleMenu(menu.getMenu_id());
        List<Integer> roles = this.menuDao.selectAllRole();
        Iterator var3 = roles.iterator();

        while(var3.hasNext()) {
            Integer role = (Integer)var3.next();
            this.menuDao.insertRoleMenu(role, menu.getMenu_id());
        }

        return this.queryById(menu.getMenu_id());
    }

    public boolean deleteById(Integer menuId) {
        this.menuDao.deleteRoleMenu(menuId);
        return this.menuDao.deleteById(menuId) > 0;
    }

    public List<Menu> queryAllByLoginName(String loginName) {
        return this.menuDao.findMenusByLoginName(loginName);
    }

    public List<LayUiTree> queryAllTreeByLoginName(String loginName) {
        new ArrayList();
        new ArrayList();
        List menuList;
        if (null != loginName) {
            menuList = this.queryAllByLoginName(loginName);
        } else {
            menuList = this.queryAllByLoginName((String)null);
        }

        List<LayUiTree> layUiTreeList = TreeUtil.fromMenuListToTreeList(menuList);
        return layUiTreeList;
    }

    public List<Menu> findMenusByLoginName(String loginName) {
        return this.menuDao.findMenusByLoginName(loginName);
    }

    public int deleteMenuById(Integer menuId) {
        return this.menuDao.deleteMenuById(menuId);
    }

    public Menu findParentNameId(Integer menuId) {
        Menu parent = this.menuDao.findParentNameId(menuId);
        if (parent != null) {
            return parent;
        } else {
            Menu menu = new Menu();
            menu.setMenu_name("没有上级目录");
            return menu;
        }
    }

}
